*&---------------------------------------------------------------------*
*& Report ZEX_LISTING_222                                              *
*&---------------------------------------------------------------------*
*& Created By: James Wood (james.wood@bowdarkconsulting.com)           *
*& Created On: 12/12/2008                                              *
*& Purpose:    This program contains the source code from the example  *
**             given in Listing 2.22.                                  *
*&---------------------------------------------------------------------*
REPORT ydate_demo.

*----------------------------------------------------------------------*
*       CLASS lcl_date DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_date DEFINITION.

  PUBLIC SECTION.
    METHODS:
      set_date IMPORTING im_month TYPE numc2
                         im_day TYPE numc2
                         im_year TYPE numc4,

      as_native_date RETURNING value(re_date)
                          TYPE sydatum,

      display_short_format RETURNING value(re_date)
                                TYPE string,

      display_long_format RETURNING value(re_date)
                               TYPE string,

      get_day_of_week RETURNING value(re_weekday)
                           TYPE string,
      get_month_name RETURNING value(re_month)
                          TYPE string.

  PRIVATE SECTION.
    DATA: month TYPE numc2,  "Month: 1-12
          day   TYPE numc2,                                 "Day: 1-31
          year   TYPE numc4. "Year

ENDCLASS.                    "lcl_date DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_date IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_date IMPLEMENTATION.

  METHOD set_date.
    month = im_month.
    day = im_day.
    year = im_year.
  ENDMETHOD.                    "set_date

  METHOD as_native_date.
    CONCATENATE year month day INTO re_date.
  ENDMETHOD.                    "as_native_date

  METHOD display_short_format.
    CONCATENATE month day year INTO re_date
    SEPARATED BY '/'.
  ENDMETHOD.                    "display_short_format

  METHOD display_long_format.
*   Local Data Declarations:
    DATA: lv_weekday TYPE string, "Week Day (String)
    lv_month TYPE string. "Month Name

*   Determine the day of the week & the month name:
    lv_weekday = get_day_of_week( ).
    lv_month = get_month_name( ).

*   Format the date string in longhand format:
    CONCATENATE lv_weekday ', '
                lv_month ' ' day ', ' year
           INTO re_date
     RESPECTING BLANKS.
  ENDMETHOD.                    "display_long_format

  METHOD get_day_of_week.
*   Local Data Declarations:
    DATA: lv_date       TYPE sydatum,            "Date in Native Fmt.
           lv_day       TYPE p,                  "Day Integral Value
           lt_day_names TYPE STANDARD TABLE
                          OF t246,               "Day Names
           ls_day_name  TYPE t246.               "Day Name

*   Use the functionality of the ABAP native date type
*   "D" to determine the week day (as an integer):
    lv_date = as_native_date( ).
    lv_day = lv_date MOD 7.
    IF lv_day GT 1.
      lv_day = lv_day - 1.
    ELSE.
      lv_day = lv_day + 6.
    ENDIF.

*   Use the standard function module DAY_NAMES_GET to
*   determine the name of the derived day value:
    CALL FUNCTION 'DAY_NAMES_GET'
      TABLES
        day_names           = lt_day_names
      EXCEPTIONS
        day_names_not_found = 1
        OTHERS              = 2.

    READ TABLE lt_day_names INTO ls_day_name
          WITH KEY wotnr = lv_day.
    IF sy-subrc EQ 0.
      re_weekday = ls_day_name-langt.
    ENDIF.
  ENDMETHOD.                    "get_day_of_week

  METHOD get_month_name.
*   Local Data Declarations:
    DATA: lt_month_names TYPE STANDARD TABLE
                           OF t247,              "Month Names
          ls_month_name TYPE t247.               "Month Name

*   Determine the month name:
    CALL FUNCTION 'MONTH_NAMES_GET'
      TABLES
        month_names           = lt_month_names
      EXCEPTIONS
        month_names_not_found = 1
        OTHERS                = 2.

    READ TABLE lt_month_names INTO ls_month_name
          WITH KEY mnr = month.
    IF sy-subrc EQ 0.
      re_month = ls_month_name-ltx.
    ENDIF.
  ENDMETHOD.                    "get_month_name
ENDCLASS.                    "lcl_date IMPLEMENTATION

*&---------------------------------------------------------------------*
*& START-OF-SELECTION Event                                            *
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM test_date_class.

*&---------------------------------------------------------------------*
*&      Form  test_date_class
*&---------------------------------------------------------------------*
FORM test_date_class.

* Local Data Declarations:
  DATA: lr_date TYPE REF                         "Date Object Ref.
                  TO lcl_date,
        lv_display TYPE string.                  "Date Display Value

* Create an instance of class lcl_date:
  CREATE OBJECT lr_date.

* Initialize the date to 9/13/2009:
  CALL METHOD lr_date->set_date
    EXPORTING
      im_month = '9'
      im_day   = '13'
      im_year  = '2009'.

* Display the date in shorthand format (e.g. mm/dd/yyyy):
  lv_display = lr_date->display_short_format( ).
  WRITE: / 'Date in shorthand format:', lv_display.

* Display the date in longhand format:
  lv_display = lr_date->display_long_format( ).
  WRITE: / 'Date in longhand format:', lv_display.

ENDFORM.                    "test_date_class